package message;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import comunity.Status;

public class StatusNotification extends Packet {

	private String receiver;
	private String owner;
	private String status;
	
	private static final String RECEIVER_ATTRIBUTE = "receiver";
	private static final String OWNER_ATTRIBUTE = "owner";	
	private static final String STATUS_ATTRIBUTE = "status";
	
	public StatusNotification(String receiver, String owner, String status, int port, double lat, double lon) {
		super(PacketType.STATUS_NOTIFICATION, port, lat, lon);
		this.receiver = receiver;
		this.owner = owner;
		this.status = status;
	}
	
	public static StatusNotification createStatusNotification(String xmlString){
		Document doc = Packet.createXMLDocumentFromString(xmlString);

		NodeList list = doc.getElementsByTagName(PacketType.STATUS_NOTIFICATION.toString());
		Node rootNode = list.item(0);
		
		Node connectionNode = rootNode.getFirstChild();
		NamedNodeMap rootAttributes = rootNode.getAttributes();
		NamedNodeMap connectionAttributes = connectionNode.getAttributes();
		
		return new StatusNotification(
				rootAttributes.getNamedItem(RECEIVER_ATTRIBUTE).getNodeValue(),
				rootAttributes.getNamedItem(OWNER_ATTRIBUTE).getNodeValue(),
				rootAttributes.getNamedItem(STATUS_ATTRIBUTE).getNodeValue(),
				Integer.parseInt(connectionAttributes.getNamedItem(Packet.PORT_ATTRIBUTE).getNodeValue()),
				Double.parseDouble(connectionAttributes.getNamedItem(Packet.LAT_ATTRIBUTE).getNodeValue()),
				Double.parseDouble(connectionAttributes.getNamedItem(Packet.LON_ATTRIBUTE).getNodeValue())
				);
	}

	@Override
	public String toXML() {
		Document doc = null;
		try {
			doc = super.createXMLDocument();
		} catch (ParserConfigurationException e1) {
			e1.printStackTrace();
		}
		
	    Element root = doc.createElement(rootElementTag());
	    root.setAttribute(StatusNotification.RECEIVER_ATTRIBUTE, this.receiver);
	    root.setAttribute(StatusNotification.OWNER_ATTRIBUTE, this.owner);
	    root.setAttribute(StatusNotification.STATUS_ATTRIBUTE, this.status.toString());
	    doc.appendChild(root);
	    
	    Element connectionElement = super.createConnectionElement(doc);
	    root.appendChild(connectionElement);
	    
	    String result = null;
	    try {
			result = super.generateString(doc);
		} catch (TransformerFactoryConfigurationError | TransformerException e) {
			e.printStackTrace();
		}
	    return result;
	}

	@Override
	String rootElementTag() {
		return PacketType.STATUS_NOTIFICATION.toString(); 
	}

	public Status getStatus() {
		if(status.equals(Status.ONLINE.toString()))
			return Status.ONLINE;
		if(status.equals(Status.OFFLINE.toString()))
			return Status.OFFLINE;
		return Status.BUSY;
	}

	@Override
	public String getReceiver() {
		return this.receiver;
	}
	
	public String getOwner() {
		return owner;
	}

}
